package com.rr.singleton;

//懒汉式
public class LazyMan2 {

    //私有的静态属性  volatile: 防止指令重排
    private volatile static LazyMan2 lazyMan2;

    //私有的构造方法
    private LazyMan2(){
        System.out.println(Thread.currentThread().getName()+"ok");
    }

    //公共的静态入口
    public static LazyMan2 getInstance(){
        //加锁
        if(lazyMan2==null){
            synchronized (LazyMan2.class){
                if(lazyMan2==null){
                    lazyMan2=new LazyMan2();//不是原子性操作
                    /**
                     * 1.分配内存空间
                     * 2.执行构造方法,初始化对象
                     * 3.把这个对象指向这个空间
                     */
                }
            }
        }
        return lazyMan2;
    }

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            new Thread(()->{
                LazyMan2.getInstance();
            }).start();
        }
    }
}
